package com.akkainaction.router.group.actor.resizepool;

import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.dispatch.OnComplete;
import akka.pattern.Patterns;
import akka.routing.GetRoutees;
import akka.routing.Routee;
import akka.routing.Routees;
import akka.util.Timeout;
import com.typesafe.config.ConfigFactory;
import scala.collection.Iterator;
import scala.collection.immutable.Iterable;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class TestMasterRouterActorByConf {

    public static void main(String[] args) throws InterruptedException {
        ActorSystem system = ActorSystem.create("sys", ConfigFactory.load("resizer-pool-router.conf"));
        ActorRef masterRouterActor = system.actorOf(Props.create(MasterRouterActorByConf.class), "masterRouterActorByConf");
//        for (int i = 0; i < 100; i++) {
//            masterRouterActor.tell("hello A" + i, ActorRef.noSender());
//        }
//        Iterable<String> elements = masterRouterActor.path().elements();
//        Iterator<String> iterator = elements.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//        System.out.println(elements.size());
//        //睡眠30秒，为的是让actor的数量降下来
//        TimeUnit.SECONDS.sleep(30);
//        for (int i = 0; i < 10; i++) {
//            masterRouterActor.tell("hello A" + i, ActorRef.noSender());
//        }
//        System.out.println(elements.size());

//        ActorSelection actorSelection = system.actorSelection("/user/**");
//        System.out.println(actorSelection.path());
        Timeout timeout = new Timeout(Duration.create(10, "seconds"));
        Future<Object> fu = Patterns.ask(masterRouterActor, GetRoutees.getInstance(), timeout);

        fu.onComplete(new OnComplete<Object>() {
            @Override
            public void onComplete(Throwable err, Object result) throws Throwable {
                Routees rs = (Routees) result;
                List<Routee> listRoutees = rs.getRoutees();
                for (Routee r : listRoutees) {
                    System.out.println("routee:" + r);
                }
            }
        }, system.dispatcher());
    }
}
